package com.xxxx.server.mq.send;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

/**
 * 邮件发送管理器
 */
@Component
public class SendMailManager {

    @Value("${spring.mail.username}")
    private String from;

    @Autowired
    private JavaMailSender javaMailSender;

    /**
     * 发送简单邮件
     * @param to 目标邮箱地址
     * @return
     */
    public boolean sendSimpleMail(String to){
        //邮件标题
        String title = "员工入职通知";
        //邮件正文
        String content = "欢迎加入哇哈哈公司！";

        //邮件参数设置
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        //邮件发送者
        simpleMailMessage.setFrom(from);
        //邮件接受者
        simpleMailMessage.setTo(to);
        //设置标题
        simpleMailMessage.setSubject(title);
        //设置内容
        simpleMailMessage.setText(content);

        //发送邮件
       try{
            javaMailSender.send(simpleMailMessage);
            return true;
        }catch (MailException e){
            e.printStackTrace();
            return false;
        }

    }
}
